/**
 * setTimeout 的 promise 封装
 * @param {Number} time
 * @returns
 */
export function timeout(time) {
  return new Promise(resolve => {
    setTimeout(resolve, time)
  })
}

export function inArray(ele, array) {
  let i = array.indexOf(ele)
  let o = {
    include: i !== -1,
    index: i
  }
  return o
}

function getDateTimeStamp(dateStr) {
  // 如果时间格式为2020/07/09 21:43:19.000  需要去掉.000 不然ios和firefox会有问题
  return Date.parse((dateStr.replace(/-/gi, "/")));
}
export function getDateDiff(dateStr) {
  var publishTime = new Date(dateStr),
    d_seconds,
    d_minutes,
    d_hours,
    d_days,
    timeNow = parseInt(new Date().getTime() / 1000),
    d;
  alert(publishTime)

  let date = publishTime,
    Y = date.getFullYear(),
    M = date.getMonth() + 1,
    D = date.getDate(),
    H = date.getHours(),
    m = date.getMinutes(),
    s = date.getSeconds();
  //小于10的在前面补0
  if (M < 10) {
    M = '0' + M;
  }
  if (D < 10) {
    D = '0' + D;
  }
  if (H < 10) {
    H = '0' + H;
  }
  if (m < 10) {
    m = '0' + m;
  }
  if (s < 10) {
    s = '0' + s;
  }

  d = timeNow - parseInt(publishTime.getTime() / 1000);
  d_days = parseInt(d / 86400);
  d_hours = parseInt(d / 3600);
  d_minutes = parseInt(d / 60);
  d_seconds = parseInt(d);

  if (d_days > 0 && d_days < 5) {
    alert(d_days)

    return d_days + '天前';
  } else if (d_days <= 0 && d_hours > 0) {
    return d_hours + '小时前';
  } else if (d_hours <= 0 && d_minutes > 0) {
    return d_minutes + '分钟前';
  } else if (d_seconds < 60) {
    if (d_seconds <= 0) {
      return '刚刚';
    } else {
      return d_seconds + '秒前';
    }
  } else if (d_days >= 3 && d_days < 30) {
    return M + '-' + D + ' ' + H + ':' + m;
  } else if (d_days >= 30) {
    return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
  }
}

export function getTheme(themeTag, cssAttrTag) {
  if (cssAttrTag === 0) {
    let bgColor = '#45a6f1';
    if (themeTag === 0)
      bgColor = '#45a6f1';
    else if (themeTag === 1)
      bgColor = '#fff';
    else
      bgColor = '#45a6f1';

    return bgColor;
  } else if (cssAttrTag === 1) {
    let color = '#fff';
    if (themeTag === 0)
      color = '#fff';
    else if (themeTag === 1)
      color = '#555';
    else
      color = '#fff';
    return color;
  }
}
